package com.meichis.yndms.ui.activity;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.DealerPortUsageAdapter;
import com.meichis.yndms.databinding.ActivityDealerPortUsageBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.DealerPortUsageVM;

import java.util.ArrayList;

public class DealerPortUsageActivity extends BaseActivity {

    private ActivityDealerPortUsageBinding binding;
    private DealerPortUsageVM vm;
    private DealerPortUsageAdapter adapter;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_dealer_port_usage);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(DealerPortUsageVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("端口出勤");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        adapter = new DealerPortUsageAdapter(this, R.layout.item_dealer_port_usage, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);

        addObserver();
        vm.DealerPortUsage();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getDealerPortUsageList().observe(this, dealerPortUsages -> {
            adapter.setList(dealerPortUsages);
        });
    }

    public class ClickEvent {

        public void onSelectDateType() {
            new DropDownDialog<>(DealerPortUsageActivity.this, R.color.theme, "时间维度", "Name", vm.getDataTableList(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getDataTable().setValue(vm.getDataTableList().get(position));
                    vm.DealerPortUsage();
                }
            }).show();
        }
    }
}
